//封装倒计时函数

import { ref, onMounted, computed, onUnmounted } from 'vue'
import dayjs from 'dayjs'
export const userCountdown = () => {
    let timer = null
    //1.响应式数据
    const time  = ref(0)
    //时间格式化
    const formatTime = computed(() => dayjs.unix(time.value).format('mm分:ss秒'))
    //2.开启倒计时
    const countdown = (currentTime) => {
        time.value = currentTime

        timer = setInterval(() => {
            time.value -= 1
        }, 1000);
    }
    //销毁倒计时
    onUnmounted(() => {
        timer&&clearInterval(timer)
    })
    return{
        formatTime ,
        countdown
    }
}